package com.zhu.mapper;

import com.zhu.pojo.Business;
import com.zhu.pojo.CustBus;
import com.zhu.pojo.Customer;
import jakarta.persistence.criteria.CriteriaBuilder;
import org.apache.ibatis.annotations.*;

import java.util.List;
import java.util.Map;

@Mapper
public interface BusinessMapper {
    @Select("select id,business_name from businesses ")
    public List<Map<String,Object>> searchNames();

    @Select("select id from businesses where business_name=#{name}")
    public Integer getbusid(String name);

    @Select("select id from customers where name=#{name}")
    public Integer getcustid(String name);

    @Select("SELECT COUNT(1) FROM customer_business WHERE customer_id = #{customer_id} AND business_id = #{business_id}")
    int checkIfExists(CustBus custBus);

    @Insert("insert into customer_business (customer_id,business_id,create_time,operator)" +
            " values (#{customer_id},#{business_id},#{create_time},#{operator})")
    public void custlinkbus(CustBus custBus);

    @Select("select business_id from customer_business where customer_id=#{id}")
    public List<Integer> getallbusid(Integer id);

    List<Business> findBusinessesByNames(List<Integer> ids);

    @Update("update businesses set install_address=#{install_address},order_number=#{order_number},order_type=#{order_type} where business_name=#{business_name}")
    public void  update(Business business);

    @Delete("delete from businesses where business_name=#{business_name}")
    public void delete(Business business);

    @Delete("delete from customer_business where business_id=#{id}")
    public void deleteCustBus(int id);

    @Select("select * from businesses where business_name like concat('%',#{business_name},'%') " +
            "and install_address like concat('%',#{install_address},'%') " +
            "and order_number like concat('%',#{order_number},'%') " +
            "and order_type like concat('%',#{order_type},'%')")
    public List<Map<String,Object>> search(Business business);

    @Select("select customer_id from customer_business where business_id=#{id}")
    public List<Integer> searchcustid(Integer id);

    List<Customer> findCustomerByIds(List<Integer> ids);

    @Select("SELECT " +
            "    order_type," +
            "    SUM(CASE WHEN business_status = '正式业务' THEN 1 ELSE 0 END) AS total_business_count," +
            "    SUM(CASE WHEN business_status = '正式业务' AND MONTH(order_receive_date) = MONTH(CURRENT_DATE()) THEN 1 ELSE 0 END) AS new_business_count " +
            "FROM businesses GROUP BY order_type")
    public List<Map<String,Object>> getstaticlist();

}
